class <%= controller_class_name %>Controller < ApplicationController
  <% @branch_name = args[0]-%> <% @leaf_name = args[1]-%><% @Branch_capital=@branch_name.capitalize-%> <% @Leaf_capital=@leaf_name.capitalize-%><% @Branch_plural=@branch_name.pluralize-%><% @Leaf_plural=@leaf_name.pluralize-%>

  def index
    @branches = <%=@Branch_capital %>.find(:all)
  end
  
  def show  
    @branches=<%=@Branch_capital%>.find(:all)
    render :partial => 'show_ajax_tree'
  end
  
  def receive_dragged_<%=@leaf_name%>
    
    arr=params[:id].split('@')
    source_branch=arr[1]
    destination_branch=params[:branch]
    leaf=arr[2]
    
    @branch_source=<%=@Branch_capital%>.find(source_branch)
    @branch_destination=<%=@Branch_capital%>.find(destination_branch)
    @leaf_to_move=<%=@Leaf_capital%>.find(leaf)
    @branch_source.<%=@Leaf_plural%>.delete(@leaf_to_move)
    @branch_destination.<%=@Leaf_plural%> << <%=@Leaf_capital%>.find(leaf)
    redirect_to :action => "show"
  
  
  end
  
  def add_<%=@leaf_name%>_to_<%=@branch_name%>
    @branch=<%=@Branch_capital%>.find(params[:branch_id])
    @branch.<%=@Leaf_plural%> << <%=@Leaf_capital%>.create(:name => params[:<%=@leaf_name%>_name])
    redirect_to :action => "show"
  end
  
   def add_<%=@branch_name%>
    <%=@Branch_capital%>.create(:name => params[:<%=@branch_name%>_name])
    redirect_to :action => "show"
  end

  def delete_<%=@leaf_name%>_from_<%=@branch_name%>
    @deleted_leaf=<%=@Leaf_capital%>.find(params[:leaf_id]).destroy
    @branch_source=<%=@Branch_capital%>.find(params[:branch_id]).<%=@Leaf_plural%>.delete(@deleted_leaf) 
    redirect_to :action => "show"
  end
  
  def delete_<%=@branch_name%>
    @branch_source=<%=@Branch_capital%>.find(params[:branch_id])
    @leafID = @branch_source.<%=@Leaf_plural%>.collect{|m| m.<%=@leaf_name%>_id} 
    <%=@Leaf_capital%>.delete(@leafID)
    @branch_source.destroy
    redirect_to :action => "show"
  end
end 
